package tv.youxing.android.ui;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView.ScaleType;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.ArrayList;

import tv.youxing.android.R;
import tv.youxing.android.custom.SmoothImageView;

public class SpaceImageDetailActivity extends Activity {

	private ArrayList<String> mDatas;
	private String urlStr;
	private int mPosition;
	private int mLocationX;
	private int mLocationY;
	private int mWidth;
	private int mHeight;
	SmoothImageView imageView = null;
	private DisplayImageOptions options;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		options = new DisplayImageOptions.Builder()	// 设置图片下载期间显示的图片
				.showImageForEmptyUri(R.drawable.no_photo)	// 设置图片Uri为空或是错误的时候显示的图片
				.showImageOnFail(R.drawable.no_photo)		// 设置图片加载或解码过程中发生错误显示的图片
				.cacheInMemory(true)						// 设置下载的图片是否缓存在内存中
				.cacheOnDisc(true)							// 设置下载的图片是否缓存在SD卡中
				.displayer(new RoundedBitmapDisplayer(5))	// 设置成圆角图片
				.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
				.build();

		mDatas = (ArrayList<String>) getIntent().getSerializableExtra("images");
		urlStr = getIntent().getStringExtra("imgurl");
		mPosition = getIntent().getIntExtra("position", 0);
		mLocationX = getIntent().getIntExtra("locationX", 0);
		mLocationY = getIntent().getIntExtra("locationY", 0);
		mWidth = getIntent().getIntExtra("width", 0);
		mHeight = getIntent().getIntExtra("height", 0);

		imageView = new SmoothImageView(this);
		imageView.setOriginalInfo(mWidth, mHeight, mLocationX, mLocationY);
		imageView.transformIn();
		imageView.setLayoutParams(new ViewGroup.LayoutParams(-1, -1));
		imageView.setScaleType(ScaleType.FIT_CENTER);

		setContentView(imageView);
//		PicassoUtils.loadImageView(this,mDatas.get(mPosition),imageView);
		//PicassoUtils.loadImageView(this,urlStr,imageView);

		ImageLoader.getInstance().displayImage(urlStr,imageView,options);


		imageView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				imageView.setOnTransformListener(new SmoothImageView.TransformListener() {
					@Override
					public void onTransformComplete(int mode) {
						if (mode == 2) {
							finish();
						}
					}
				});
				imageView.transformOut();
			}
		});

//		ImageLoader.getInstance().displayImage(mDatas.get(mPosition), imageView);
//		imageView.setImageResource(R.drawable.logo);
//		 ScaleAnimation scaleAnimation = new ScaleAnimation(0.5f, 1.0f, 0.5f,
//		 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
//		 0.5f);
//		 scaleAnimation.setDuration(300);
//		 scaleAnimation.setInterpolator(new AccelerateInterpolator());
//		 imageView.startAnimation(scaleAnimation);

	}

	@Override
	public void onBackPressed() {
		imageView.setOnTransformListener(new SmoothImageView.TransformListener() {
			@Override
			public void onTransformComplete(int mode) {
				if (mode == 2) {
					finish();
				}
			}
		});
		imageView.transformOut();
	}

	@Override
	protected void onPause() {
		super.onPause();
		if (isFinishing()) {
			overridePendingTransition(0, 0);
		}
	}


	@Override
	protected void onDestroy() {
		imageView.setImageBitmap(null);
		imageView.setImageDrawable(null);
		imageView = null;
		super.onDestroy();
	}
}
